﻿@using SDCloudSolution.SDCloud.Web.Utils
@using SDCloudSolution.SDCloud.Web.Models
@model SDCloudSolution.SDCloud.Web.Models.Model.ModelViewModel

@{
    Layout = MVC.Shared.Views._AuthorizedLayout;
    ViewBag.Title = Model.Name;
}

@section scripts {
    <script src="~/Scripts/js/libs/require/require.js"></script>
    <script src="~/Scripts/app.js"></script>
    <script src="~/Scripts/sdCloud/model.view.js"></script>
}

<h1 class="page-header">
    <a href="@Url.Action(MVC.Models.List())">
        <img src="~/Content/left arrow.png" width="24" height="24"></a>
    @Model.Name
    
    @if (Model.IsPublic)
    {
        <small>(This model shared to public <a href="@Url.Action(MVC.ModelsGallery.View(Model.ModelId))">gallery</a>)</small>
    }
    
    @if (Model.OriginalModelId.HasValue)
    {
        <small>(This model was copied from <a href="@Url.Action(MVC.ModelsGallery.View(Model.OriginalModelId.Value))">gallery</a>)</small>
    }
</h1>

<div id="model-buttons-container" class="oj-flex oj-sm-flex-items-initial">
    <div class="oj-flex-item">
        @if (Model.CanRunModel)
        {
            using (Html.BeginForm(MVC.ModelRuns.QuickStart(Model.ModelId), FormMethod.Post, new { style = "display: inline-block;" }))
            {
                <button id="start" type="submit" class="oj-button-confirm"
                        data-bind="ojComponent: {
                            component: 'ojButton', 
                            label: 'Start',
                            icons: { start: 'fa fa-play' }
                        }">
                    Start
                </button>
            }
        }
        
        @if (Model.CanStopLastModelExecution)
        {
            using (Html.BeginForm(MVC.ModelRuns.Cancel(Model.LastModelRunItem.ModelRunId), FormMethod.Post, new { style = "display: inline-block;" }))
            {
                <button id="stop" type="submit" class="button-error"
                        data-bind="ojComponent: {
                        component: 'ojButton', 
                        label: 'Stop',
                        icons: { start: 'fa fa-stop' }
                        }">Stop</button>
            }
        }
        
        <a id="modelRuns" href="@Url.Action(MVC.ModelRuns.ListForModel(Model.ModelId))"
           data-bind="ojComponent: {
                      component: 'ojButton',
                      icons: {start:'fa fa-history'}
                  }">All model runs</a>
    </div>
    
    <div class="oj-helper-margin-start-auto oj-flex-item">
        <a id="edit" href="@Url.Action(MVC.Models.Edit(Model.ModelId))"
           data-bind="ojComponent: {
                      component: 'ojButton',
                      icons: {start:'fa fa-pencil-square-o'}
                      }">Edit</a>

        <form method="POST" action="@Url.Action(MVC.Models.Delete(Model.ModelId))" style="display: inline-block;">
            <button id="delete" type="submit" class="button-error"
                    data-bind="ojComponent: {
                      component: 'ojButton',
                       label: 'Delete',
                      icons: {start:'fa fa-trash-o'}
                  }">
                Delete
            </button>
        </form>
    </div>
</div>

@if (Model.Metadata != null)
{
    <h3>Model metadata</h3>
    <div class="oj-flex oj-flex-items-pad oj-panel">
        @if (@Model.Metadata.Name != null)
        {
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Model name</div>
            <div>
                 @Model.Metadata.Name
            </div>
        </div>
        }
        @if (@Model.Metadata.Name != null)
        {
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Model vendor</div>
            <div>
                @Model.Metadata.Vendor
            </div>
        </div>
        }
    </div>

    if (Model.Metadata.ConfigurableStocks.Count != 0 ||
            Model.Metadata.ConfigurableFlows.Count != 0 ||
            Model.Metadata.ConfigurableAuxes.Count != 0)
    {
    <h3>Model configuration</h3>
    <div class="oj-flex oj-flex-items-pad oj-panel">        
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Configurable stocks</div>
            @foreach (KeyValuePair<string, object> kvp in Model.Metadata.ConfigurableStocks)
            {
                <div>@kvp.Key : @kvp.Value</div>
            }
        </div>

        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Configurable flows</div>
            @foreach (KeyValuePair<string, object> kvp in Model.Metadata.ConfigurableFlows)
            {
                <div>@kvp.Key : @kvp.Value</div>
            }
        </div>

        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Configurable auxes</div>
            @foreach (KeyValuePair<string, object> kvp in Model.Metadata.ConfigurableAuxes)
            {
                <div>@kvp.Key : @kvp.Value</div>
            }
        </div>

    </div>
    }
}


@if (Model.ModelStatistics != null)
{
    <h3>Model statistics</h3>
    <div class="oj-flex oj-flex-items-pad oj-panel">
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Model success run count</div>
            <div>
                @Model.ModelStatistics.TotalSuccessExecutionCount (@Model.ModelStatistics.TotalSuccessExecutionPercentage.ToString("P1"))
            </div>
        </div>
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Model failed run count</div>
            <div>
                @Model.ModelStatistics.TotalFailedExecutionCount (@Model.ModelStatistics.TotalFailedExecutionPercentage.ToString("P1"))
            </div>
        </div>
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Model total run count</div>
            <div>@Model.ModelStatistics.TotalExecutionCount</div>
        </div>
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Average execution time</div>
            <div>@Model.ModelStatistics.AverageExecutionTimeText</div>
        </div>
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Average data usage</div>
            <div>@Model.ModelStatistics.AverageDataSizeUsageText</div>
        </div>
    </div>
}

@if (Model.LastModelRunItem != null)
{
    <h3>Previous model run info</h3>
    <div class="oj-flex oj-flex-items-pad oj-panel">
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Queued</div>
            <div>
                @Model.LastModelRunItem.QuequedDate
            </div>
        </div>
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Status</div>
            <div>
                <span class="label label-@Model.LastModelRunItem.StatusType.ToCssClass()">@Model.LastModelRunItem.StatusTitle</span>
            </div>
        </div>
        <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
            <div class="oj-header-border bold">Elapsed Time</div>
            <div>
                <span>@Model.LastModelRunItem.ElapsedTime</span>
            </div>
        </div>
        @if (Model.LastModelRunItem.StatusType.Equals(StatusViewModel.Success))
        {
            <div class="oj-flex-item oj-margin oj-sm-12 oj-lg-3 oj-md-4 oj-xl-2">
                <div class="oj-header-border bold">Last model run Result</div>
                <div>
                    <a href="@Url.Action(MVC.ModelRuns.DownloadAsJson(Model.LastModelRunItem.ModelRunId))" target="_blank">
                        Download as .json
                    </a>
                </div>
                <div>
                    <a href="@Url.Action(MVC.ModelRuns.DownloadAsCsv(Model.LastModelRunItem.ModelRunId))" target="_blank">
                        Download as .csv
                    </a>
                </div>
            </div>
        }        
    </div>
}

<h3>Model description</h3>
<div class="oj-flex oj-flex-items-pad oj-panel">
    @Model.FormattedDescription
</div>

<h3>File</h3>
<div class="oj-flex oj-flex-items-pad oj-panel">
    <b>@Html.ActionLink(@Model.FileName, MVC.Models.Download(Model.ModelId))</b>
</div>